﻿<cfscript>
/** 
* @hint "chooseManager - ChooseTurn"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.chooseTurnListener" 
	output=false 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "保存选课轮次设置"
	* 
	* @event "MachII Event Object"
	*/
	public void function saveTurnSetting( required MachII.framework.Event event ) output=false {
		
		var turnEntity = "";
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var gradesAllowed = "";
			var instituteAllowed = "";
			
			var dateStart = replace(event.getArg("DateStart"), "-", "", "ALL");
			var dateEnd = replace(event.getArg("DateEnd"), "-", "", "ALL");
			
			var timeStart = replace(event.getArg("TimeStart"), ":", "", "ALL");
			var timeEnd = replace(event.getArg("TimeEnd"), ":", "", "ALL");
			
			/* 选课时间段 */
			if ( event.getArg("FullDay") eq "1" ) {
				timeStart = "0001";
				timeEnd = "2359";
			}

			/* 允许选课年级 */
			if ( event.getArg("FullGrade") eq "1" ) {
				gradesAllowed = "ALL";
			}
			else {
				gradesAllowed = event.getArg('targetGrade');
			}

			var subjectIds = event.getArg('targetSubject','-');

			/* 允许选课学院 */
			if ( event.getArg("FullInstitute") eq "1" ) {
				instituteAllowed = "ALL";
				subjectIds = '-';
			}else {
				var targetInstitutes = event.getArg('targetInstitute','-');
				instituteAllowed = targetInstitutes;
				
				if(targetInstitutes neq '-' and subjectIds neq '-'){
					subjectIdArray = listToArray(subjectIds,",");
					targetInstituteArray = listToArray(targetInstitutes,",");
					if(arrayLen(targetInstituteArray)){
						for(i = 1;i lte arrayLen(targetInstituteArray);i++){
							sql = "	SELECT
										sbj.sbj_id
									FROM
										t_subject sbj
									WHERE
										sbj.institute_id = :instituteId ";
							
							queryObj = new Query(datasource = application.dnsMaster);
							queryObj.addParam(name = "instituteId", value = targetInstituteArray[i], cfsqltype = "cf_sql_varchar");
							rs_InSubjects = queryObj.execute(sql = sql).getResult();
							
							if(rs_InSubjects.recordCount){
								for(j=1;j lte rs_InSubjects.recordCount;j++){
									if(arrayFind(subjectIdArray,rs_InSubjects['sbj_id'][j])){
										arrayDelete(subjectIdArray,rs_InSubjects['sbj_id'][j]);
									}
								}
							}
						}
					}
					if(arrayLen(subjectIdArray)){
						subjectIds = arrayToList(subjectIdArray,",");
					}else{
						subjectIds = '-';	
					}
				}
			}
			
			sql = "	UPDATE
						t_choose_turn ct
					SET
						ct.date_start = :dateStart ,
						ct.date_end = :dateEnd ,
						ct.time_start = :timeStart ,
						ct.time_end = :timeEnd ,
						ct.grade_allowed = :gradesAllowed ,
						ct.institute_allowed = :instituteAllowed ,
						ct.subject_allowed = :subjectIds 
					WHERE
						ct.ct = :ct ";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "dateStart", value = dateStart, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "dateEnd", value = dateEnd, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "timeStart", value = timeStart, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "timeEnd", value = timeEnd, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "gradesAllowed", value = gradesAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "instituteAllowed", value = instituteAllowed, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "subjectIds", value = subjectIds, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "ct", value = event.getArg("TurnID"), cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
		}
		
	}
	
}

</cfscript>